<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('指标科目/监控') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('监控科目/指标')" prop="keyCode">
        <treeselect
          v-model="queryParams.keyCode"
          :options="sysKeyOptions"
          :disable-branch-nodes="true"
          :placeholder="$t('监控科目/指标')"
          @keyup.enter.native="queryHandle"
        >
          <label slot="option-label" slot-scope="{ node, shouldShowCount, count, labelClassName, countClassName }" :class="labelClassName">
            {{ $t(node.label) }}
            <span v-if="shouldShowCount" :class="countClassName">({{ count }})</span>
          </label>
          <label slot="value-label" slot-scope="{ node }">
            {{ $t(node.label) }}
          </label>
        </treeselect>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['company:pcw:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          v-hasPermi="['company:pcw:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>-->
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="pcwRiskMonitorList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('监控科目/指标')" align="center" prop="keyName">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ $t(scope.row.keyName) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('上下行预警')" align="center" prop="warmUpdown" :formatter="riskTypeFormat" width="120" />
      <el-table-column :label="$t('预警线')" align="center" prop="warnLine" width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.warnLine) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('警报线')" align="center" prop="alarmLine" width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.alarmLine) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('最新值')" align="center" prop="latestValue" min-width="80">
        <template v-slot="scope">
          <span>{{ moneyFormat(scope.row.latestValue) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('报警次数')" align="center" prop="riskType" width="130">
        <template slot-scope="scope">
          <span class="riskSpan">
            <span class="risk-type-red" />
            <i> {{ scope.row.alarmCount }} </i>
          </span>
          <span class="riskSpan">
            <span class="risk-type-yellow" />
            <i>{{ scope.row.warmCount }}</i>
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('关键条款')" align="center" prop="termsLabel" />
      <el-table-column :label="$t('已处理数')" align="center" prop="handleCount" width="100" />
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['company:pcw:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['company:pcw:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改指标监控对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <pcwRiskMonitorForm ref="pcwRiskMonitorForm" :company-id="companyId ? companyId : this.$route.query.companyId" :deal-id="this.$route.query.id" />
    </el-dialog>
  </div>
</template>

<script>
import { listPcwRiskMonitor, delPcwRiskMonitor, exportPcwRiskMonitor } from '@/api/company/pcw/pcwRiskMonitor'
import pcwRiskMonitorForm from './pcwRiskMonitorForm.vue'
import { listSubjectTree } from '@/api/company/pcw/pcwTempSubject'
import Treeselect from '@riophae/vue-treeselect'
export default {
    components: {
        pcwRiskMonitorForm,
        Treeselect
    },
  props: {
    companyId: {
      type: String,
      required: false,
      default: undefined
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 指标监控表格数据
          pcwRiskMonitorList: [],
          // 弹出层标题
          title: '指标监控',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              companyId: this.companyId ? this.companyId : this.$route.query.companyId,
              keyCode: undefined,
              keyName: undefined,
              warmUpdown: undefined,
              alarmLine: undefined,
              warnLine: undefined,
              alarmCount: undefined,
              warmCount: undefined,
              handleCount: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 查询科目下拉框参数
          querySubject: {
            companyId: this.companyId ? this.companyId : this.$route.query.companyId
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          },
          // 科目类型
          sysKeyOptions: [
            { id: 'balance_sheet', label: '资产负债表', children: [] },
            { id: 'income_sheet', label: '利润表', children: [] },
            { id: 'cash_flow_sheet', label: '现金流量表', children: [] }
          ]
      }
  },
  watch: {
    companyId: {
      handler(val) {
        if (val) {
          this.queryParams.companyId = val
          this.getList()
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.getDicts('risk_monitor_type').then(response => {
      this.riskTypeOptions = response.data
    })
    // 根据选择的tableName,获取下面的默认指标
    listSubjectTree(this.querySubject).then(response => {
      const item = response.rows
      this.diGuiTree(item)
      this.sysKeyOptions = item
    })
  },
  mounted() {
    if (!this.companyId) {
      this.queryParams.companyId = this.$route.query.companyId
      this.querySubject.companyId = this.queryParams.companyId
    }
    this.getList()
  },
  methods: {
    // 注册资本币种字典翻译
    riskTypeFormat(row) {
      return this.selectDictLabel(this.riskTypeOptions, row.warmUpdown)
    },
    /** 查询指标监控列表 */
    getList() {
        this.loading = true
        listPcwRiskMonitor(this.queryParams).then(response => {
            this.pcwRiskMonitorList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 递归函数的具体写法：
    diGuiTree(item) {
      item.forEach(item => {
        if (item.children === '' || item.children === undefined || item.children === null || item.children.length === 0) {
          delete item.children
        } else {
          this.diGuiTree(item.children)
        }
      })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: this.companyId ? this.companyId : this.$route.query.companyId,
            keyCode: undefined,
            keyName: undefined,
            warmUpdown: undefined,
            alarmLine: undefined,
            warnLine: undefined,
            alarmCount: undefined,
            warmCount: undefined,
            handleCount: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.pcwRiskMonitorForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.pcwRiskMonitorForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delPcwRiskMonitor(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportPcwRiskMonitor(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    }
  }
}
</script>
